<?php
    /* functions */
    function checkTelcoMeCorp($mobile)
    {        
        $first = substr($mobile, 0, 1);
        if($first=="0"){
            $mobile = substr($mobile, 1, strlen($mobile) - 1);
        } else if($first="8"){
            $mobile = substr($mobile, 2, strlen($mobile) - 2);
        }
        
        $mobile = "84" . $mobile;
        
        $patternViettel = "/^84(9[678]|16[2-9])[\d]{7}/";
        $patternMobi = "/^84(9[03]|12[01268])[\d]{7}/";
        $patternVina = "/^84(9[14]|12[34579])[\d]{7}/";
        $patternVnmobile = "/^84(9[2]|18[86])[\d]{7}/";
        $patternBeeline = "/^84(9[9]|19[9])[\d]{7}/";
        
        if(preg_match($patternViettel, $mobile)){
            $telco = "VTEL";    
        } else if(preg_match($patternMobi, $mobile)){
            $telco = "VMS";
        } else if(preg_match($patternVina, $mobile)){
            $telco = "VNP";
        } else if(preg_match($patternVnmobile, $mobile)){
            $telco = "VNMOBILE";
        } else if(preg_match($patternBeeline, $mobile)){
            $telco = "BEELINE";
        } else {
            $telco = "";
        }
        
        return $telco;    
    }
    
    function serviceMeCorp($data)
    {        
        $privateKey = "RMdasGJYI6EmmtK";
        $password = md5($data["transactionId"] . $data["sender"] . $data["serviceCode"] . $data["content"] . $privateKey);
        $route = checkTelcoMeCorp($data["sender"]);
        
        $url = "http://pay.gomobi.vn/index.php/sms/sms/partner/taoviec";
        $url .= "?username=" . urlencode("taoviec");
        $url .= "&password=" . urlencode($password);
        $url .= "&smsid=" . urlencode($data["transactionId"]);
        $url .= "&sender=" . urlencode($data["sender"]);
        $url .= "&receiver=" . urlencode($data["serviceCode"]);
        $url .= "&content=" . urlencode($data["content"]);                
        $url .= "&route=" . urlencode($route);
        $url .= "&timereceived=" . urlencode(date("Y-m-d H:i:s"));
        $url .= "&data=" . urlencode("json");
        
        $response = cUrl($url);
        $response = json_decode(utf8_decode($response));
        return $response;
    }
    
    function getUserLongtinh($sms_code)
    {
        $sql = "SELECT userId, username FROM c_longtinh WHERE `sms_code`='" . mysql_escape_string($sms_code) . "'";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    } 
    
    function buildSyntaxLongtinh($content, $regx)
    {
        preg_match($regx, $content, $match);
        
        $cp = array();
        $cp["sms_code"] = isset($match[2]) ? $match[2] : 0;
        $cp["username"] = isset($match[5]) ? $match[5] : "";
        
        /* Lấy userId */
        $user = getUserLongtinh($cp["sms_code"]);
        $cp["userId"] = !empty($user["userId"]) ? $user["userId"] : 0;
        $cp["user_name"] = !empty($user["username"]) ? $user["username"] : "";
        
        return $cp;   
    }
    
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);
        
        $cp = array();
        $cp["refCode"] = isset($match[6]) ? $match[6] : "";
        $cp["username"] = isset($match[3]) ? $match[3] : "";
        
        /* Lấy userId */
        $user = getUser($cp["refCode"]);
        $cp["userId"] = !empty($user["id"]) ? $user["id"] : 0;
        $cp["user_name"] = !empty($user["username"]) ? $user["username"] : "";
        
        return $cp;   
    }
    
    function buildSyntaxGiftCode($content, $regx)
    {
        preg_match($regx, $content, $match);
        
        $cp = array();
        $cp["refCode"] = isset($match[4]) ? $match[4] : "";
        
        /* Lấy userId */
        $user = getUser($cp["refCode"]);
        $cp["userId"] = !empty($user["id"]) ? $user["id"] : 0;
        
        return $cp;   
    }
    
    /*
    * processing
    */
    
    function processing($data)
    {
        $game_kytien_id = 151;
        $game_tuhan_id = 150;
        $game_longtinh_id = 149;
                
        $regx_active_kytien1 = "/^VAKT(\s+)ACTIVE(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
        $regx_active_kytien2 = "/^VAKT(\s+)ACTIVE(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})$/si";
        $regx_buy_kytien = "/^VAKT(\s+)BUY(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
        $regx_nap_kytien = "/^VAKT(\s+)NAP(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
        $regx_giftcode_kytien = "/^VAKT(\s+)GIFTCODE(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
        
        $regx_active_tuhan1 = "/^TUHA(\s+)ACTIVE(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
        $regx_active_tuhan2 = "/^TUHA(\s+)ACTIVE(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})$/si";
        $regx_buy_tuhan = "/^TUHA(\s+)BUY(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
        $regx_nap_tuhan = "/^TUHA(\s+)NAP(\s+)([a-zA-Z0-9_]{1,})(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
        $regx_giftcode_tuhan = "/^TUHA(\s+)GIFTCODE(.*)(\s+)([a-zA-Z0-9]{1,})(\s+)(\d+)$/si";
                
        $regx_active_longtinh = "/^VALT(\s+)(\d+)(\s+)ACTIVE(\s+)([a-zA-Z0-9_]{1,})/si";
        $regx_buy_longtinh = "/^VALT(\s+)(\d+)(\s+)BUY(\s+)([a-zA-Z0-9_]{1,})/si";
        $regx_nap_longtinh = "/^VALT(\s+)(\d+)(\s+)NAP(\s+)([a-zA-Z0-9_]{1,})/si";
        $regx_giftcode_longtinh = "/^VALT(\s+)(\d+)(\s+)GIFTCODE(\s+)([a-zA-Z0-9_]{1,})/si";        
        
        $data_reg = array();
        if(
            preg_match($regx_active_kytien1, $data["content"])
             || preg_match($regx_active_kytien2, $data["content"])
             || preg_match($regx_buy_kytien, $data["content"])
             || preg_match($regx_nap_kytien, $data["content"]) 
             || preg_match($regx_giftcode_kytien, $data["content"]) 
             
             || preg_match($regx_active_tuhan1, $data["content"])
             || preg_match($regx_active_tuhan2, $data["content"])
             || preg_match($regx_buy_tuhan, $data["content"])
             || preg_match($regx_nap_tuhan, $data["content"]) 
             || preg_match($regx_giftcode_tuhan, $data["content"]) 
             
             || preg_match($regx_active_longtinh, $data["content"])
             || preg_match($regx_buy_longtinh, $data["content"])
             || preg_match($regx_nap_longtinh, $data["content"]) 
             || preg_match($regx_giftcode_longtinh, $data["content"]) 
        ){ 
            if(preg_match($regx_active_kytien1, $data["content"])){
                
                /* active user */
                $syntax = buildSyntax($data["content"], $regx_active_kytien1);
                $list_service_code = array("6022");
                $data["id"] = $game_kytien_id;
                $data["action"] = "reg";
                
            } else if(preg_match($regx_active_kytien2, $data["content"])){    
                
                /* buy, nap */
                $syntax = buildSyntax($data["content"], $regx_active_kytien2);
                $list_service_code = array("6022");
                $data["id"] = $game_kytien_id;
                $data["action"] = "reg";
                
            } else if(preg_match($regx_nap_kytien, $data["content"])){    
                
                /* buy, nap */
                $syntax = buildSyntax($data["content"], $regx_nap_kytien);
                $list_service_code = array("6622", "6722");
                $data["id"] = $game_kytien_id;
                $data["action"] = "nap";
                
            } else if(preg_match($regx_buy_kytien, $data["content"])){    
                
                /* buy, nap */
                $syntax = buildSyntax($data["content"], $regx_buy_kytien);
                $list_service_code = array("6622", "6722");
                $data["id"] = $game_kytien_id;
                $data["action"] = "nap";
                
            } else if(preg_match($regx_giftcode_kytien, $data["content"])){    
                
                /* gift code */
                $syntax = buildSyntaxGiftCode($data["content"], $regx_giftcode_kytien);
                $list_service_code = array("6022");
                $data["id"] = $game_kytien_id;
                $data["action"] = null;
                
            } else if(preg_match($regx_active_tuhan1, $data["content"])){
                
                /* active user */
                $syntax = buildSyntax($data["content"], $regx_active_tuhan1);                
                $list_service_code = array("6022");
                $data["id"] = $game_tuhan_id;                
                $data["action"] = "reg";
                
            } else if(preg_match($regx_active_tuhan2, $data["content"])){
                
                /* active user */
                $syntax = buildSyntax($data["content"], $regx_active_tuhan2);                
                $list_service_code = array("6022");
                $data["id"] = $game_tuhan_id;                
                $data["action"] = "reg";
                
            } else if(preg_match($regx_nap_tuhan, $data["content"])){    
                
                /* buy, nap */
                $syntax = buildSyntax($data["content"], $regx_nap_tuhan);
                $list_service_code = array("6622", "6722");
                $data["id"] = $game_tuhan_id;
                $data["action"] = "nap";
                
            } else if(preg_match($regx_buy_tuhan, $data["content"])){    
                
                /* buy, nap */
                $syntax = buildSyntax($data["content"], $regx_buy_tuhan);
                $list_service_code = array("6622", "6722");
                $data["id"] = $game_tuhan_id;
                $data["action"] = "nap";
                
            } else if(preg_match($regx_giftcode_tuhan, $data["content"])){    
                
                /* gift code */
                $syntax = buildSyntaxGiftCode($data["content"], $regx_giftcode_tuhan);
                $list_service_code = array("6022");
                $data["id"] = $game_tuhan_id;
                $data["action"] = null;
                
            } else if(preg_match($regx_active_longtinh, $data["content"])){
                
                /* active user */
                $syntax = buildSyntaxLongtinh($data["content"], $regx_active_longtinh);
                $list_service_code = array("6022");
                $data["id"] = $game_longtinh_id;
                $data["action"] = "reg";
                
            } else if(preg_match($regx_nap_longtinh, $data["content"])){    
                
                /* buy, nap */
                $syntax = buildSyntaxLongtinh($data["content"], $regx_nap_longtinh);
                $list_service_code = array("6622", "6722");
                $data["id"] = $game_longtinh_id;
                $data["action"] = "nap";
                
            } else if(preg_match($regx_buy_longtinh, $data["content"])){    
                
                /* buy, nap */
                $syntax = buildSyntaxLongtinh($data["content"], $regx_buy_longtinh);
                $list_service_code = array("6622", "6722");
                $data["id"] = $game_longtinh_id;
                $data["action"] = "nap";
                
            } else if(preg_match($regx_giftcode_longtinh, $data["content"])){    
                
                /* gift code */
                $syntax = buildSyntaxLongtinh($data["content"], $regx_giftcode_longtinh);
                $list_service_code = array("6022");
                $data["id"] = $game_longtinh_id;
                $data["action"] = null;
                
            } 
            
            $data_reg["game_user"] = isset($syntax["username"]) ? $syntax["username"] : "";
            $data_reg["username"] = isset($syntax["user_name"]) ? $syntax["user_name"] : "";
             
            $data["userId"] = $syntax["userId"];
            if(in_array($data["serviceCode"], $list_service_code)){
                                                        
                $result = serviceMeCorp($data);                        
                
                $data["status"] = $result->cdr==1 ? 1 : 0;
                $response["status"] = $result->cdr;
                $response["msg"] = $result->mt;
                
            } else {
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;                 
            }
            
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
            
            $data["id"] = 0; //ID game
        } 
        
        /* log sms */
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 55; //ID partner Me Corp    
        $data["type"] = 1; //Thể loại game   
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
        
        $result = InsertSmsLog($data);   
        if($data["action"]=="reg"){
            $data_reg["userId"] = $data["userId"];    
            $data_reg["gameId"] = $data["id"];                
            
            /* Check xem user da ton tai chua */
            $result = CheckGameUser($data_reg);
            if($result==0){
                $result = InsertGameUser($data_reg);    
            }            
        }
        
        return $response;   
    }
?>
